package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.heima.behavior.service.CommentReplyService;
import com.heima.common.util.RequestContextUtil;
import com.heima.model.behavior.dtos.CommentReplyDto;
import com.heima.model.behavior.dtos.CommentReplyLikeDto;
import com.heima.model.behavior.dtos.CommentReplySearchDto;
import com.heima.model.behavior.pojos.CommentDoc;
import com.heima.model.behavior.pojos.CommentLikeDoc;
import com.heima.model.behavior.pojos.CommentReplyDoc;
import com.heima.model.behavior.pojos.CommentReplyLikeDoc;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CommentReplyServiceImpl implements CommentReplyService {
    @Autowired
    MongoTemplate mongoTemplate;
    /**
     * 评论回复——保存
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveReplyComment(CommentReplyDto dto) {
        CommentReplyDoc commentReplyDoc = new CommentReplyDoc();
        Integer userId = RequestContextUtil.get("id");
        String name=RequestContextUtil.get("name");
        commentReplyDoc.setId(IdWorker.getId());
        commentReplyDoc.setAuthorId(userId);
        commentReplyDoc.setAuthorName(name);
        commentReplyDoc.setCommentId(dto.getCommentId());
        commentReplyDoc.setContent(dto.getContent());
        commentReplyDoc.setLikes(0);
        commentReplyDoc.setCreatedTime(new Date());
        commentReplyDoc.setUpdatedTime(new Date());
        System.out.println("评论回复保存成功");
        mongoTemplate.save(commentReplyDoc);
        return ResponseResult.okResult(commentReplyDoc);
    }

    /**
     * 评论回复——查询评论列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadReplyComment(CommentReplySearchDto dto) {
        Query query = Query.query(Criteria.where("commentId").is(dto.getCommentId()));
        query.addCriteria(Criteria.where("createdTime").lt(dto.getMinDate()));
        query.with(Sort.by(Sort.Direction.DESC,"createdTime"));
        query.limit(5);
        List<CommentReplyDoc> docList = mongoTemplate.find(query, CommentReplyDoc.class);

        // 当前用户，评论点赞状态
        Integer userId=RequestContextUtil.get("id");
        for (CommentReplyDoc commentReplyDoc : docList) {//遍历评论回复列表
            Query query1 = Query.query(Criteria.where("commentRepayId").is(commentReplyDoc.getId())
                    .and("userId").is(userId));
            List<CommentReplyLikeDoc> likeDocs = mongoTemplate.find(query1, CommentReplyLikeDoc.class);//哪些评论回复被点赞
            System.out.println(likeDocs.size());
            if (likeDocs.size()>0) {
                for (CommentReplyLikeDoc likeDoc : likeDocs) {
                    if (commentReplyDoc.getId().longValue()==likeDoc.getCommentRepayId().longValue()) {
                        commentReplyDoc.setOperation(0);
                    }
                }
            }
        }
        return ResponseResult.okResult(docList);
    }

    /**
     * 评论回复——点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeReplyComment(CommentReplyLikeDto dto) {
        Integer userId = RequestContextUtil.get("id");
        Query query = Query.query(Criteria.where("commentRepayId").is(dto.getCommentRepayId())
                .and("userId").is(userId));
        List<CommentReplyLikeDoc> likeDocs = mongoTemplate.find(query, CommentReplyLikeDoc.class);
        if (likeDocs.size() <= 0) {
            mongoTemplate.save(new CommentReplyLikeDoc(IdWorker.getId(),dto.getCommentRepayId(),userId));
            System.out.println("点赞评论成功");
            long count = mongoTemplate.count(query, CommentReplyDoc.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("likes",count);
            return ResponseResult.okResult(map);
        } else {
            mongoTemplate.remove(query, CommentReplyLikeDoc.class);
            System.out.println("取消评论点赞");
            long count = mongoTemplate.count(query, CommentReplyDoc.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("likes",count);
            return ResponseResult.okResult(map);
        }
    }
}
